package com.jerry.secondkill.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import com.jerry.secondkill.domain.Order;
import com.jerry.secondkill.domain.SeckillOrder;

@Mapper
public interface OrderDao {
	
	@Select("select * "
		  + "from t_seckill_order "
		  + "where user_id=#{userId} and goods_id=#{goodsId}")
	public SeckillOrder getSeckillOrderByUserIdGoodsId(@Param("userId")long userId, @Param("goodsId")long goodsId);

	@Insert("insert into t_order(user_id, goods_id, goods_name, goods_count, goods_price, order_channel, status, create_date)"
		  + "values(#{userId}, #{goodsId}, #{goodsName}, #{goodsCount}, #{goodsPrice}, #{orderChannel},#{status},#{createDate} )")
	@SelectKey(keyColumn="id", keyProperty="id", resultType=long.class, before=false, statement="select last_insert_id()")
	public long insertOrder(Order order);
	
	@Insert("insert into t_seckill_order (user_id, goods_id, order_id)"
		  + "values(#{userId}, #{goodsId}, #{orderId})")
	public int insertSeckillOrder(SeckillOrder seckillOrder);

	@Select("select * "
		  + "from t_order "
		  + "where id = #{orderId}")
	public Order getOrderById(@Param("orderId")long orderId);
	
	@Delete("delete from t_order")
	public void deleteOrders();

	@Delete("delete from t_seckill_order")
	public void deleteSeckillOrders();
	
}
